using System;
using Microsoft.Build.BuildEngine;
using Microsoft.Build.Framework;

namespace QuickTest.Core.Compilation
{
    public class MSBuildCompiler : ICompiler
    {
        private readonly ILogger logger;

        public MSBuildCompiler(ILogger logger)
        {
            this.logger = logger;
        }

        public MSBuildCompiler()
            : this(new ConsoleLogger())
        {
        }

        public void Compile(FileUri targetFileUri)
        {
            if (targetFileUri == null)
                throw new ArgumentNullException("targetFileUri");

            var buildEngine = new Engine();

            buildEngine.RegisterLogger(logger);

            buildEngine.BuildProjectFile(targetFileUri.AbsolutePath);
            buildEngine.Shutdown();
        }
    }
}